import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '@/components/home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/Users/users.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
      },
      {
        path: '/users',
        component: User,
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../components/Rights/rights.vue'),
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../components/Goods/goods.vue'),
      },
      {
        path: '/goods/add',
        name: 'add',
        component: () => import('../components/Goods/Add.vue'),
      },
      {
        path: '/categories',
        component: () => import('../components/Categories/categories.vue'),
      },
      {
        path: '/params',
        component: () => import('../components/Params/params.vue'),
      },
      {
        path: '/orders',
        component: () => import('../components/Orders/orders.vue'),
      },
      {
        path: '/reports',
        component: () => import('../components/Reports/reports.vue'),
      },
      {
        path: '/roles',
        component: () => import('../components/Roles/roles.vue'),
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
